﻿using StudentCouncil.Model;
using StudentCouncil.Model.Models;
using StudentCouncil.Model.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentCouncil.DAL.DAO
{
   public class StudentUnionDAO
    {
        public static Pager<StudentUnion> GetListByPage(StudentUnionParameter parameter)
        {
            Pager<StudentUnion> result = new Pager<StudentUnion>();
            using (StudentContext db = new StudentContext())
            {
                var query = db.StudentUnion.AsQueryable();
                if (!string.IsNullOrEmpty(parameter.Name))
                {
                    query = query.Where(t => t.Name.Contains(parameter.Name));
                }

                result.Total = query.Count();
                result.Rows = query.OrderBy(t => t.UnionId)
                    .Skip(parameter.Skip)
                    .Take(parameter.PageSize)
                    .ToList();
                return result;
            }
        }
        
        public static List<StudentUnion> GetList()
        {
            using (StudentContext db = new StudentContext())
            {
                return db.StudentUnion.ToList();
            }
        }
        public static bool AddStudent_union(StudentUnion entity)
        {
            using (StudentContext db = new StudentContext())
            {
                db.StudentUnion.Add(entity);
                db.SaveChanges();
                return true;
            }
        }

        public static StudentUnion GetStudentById(int id)
        {
            using (StudentContext db = new StudentContext())
            {
                var res = db.StudentUnion.Find(id);
                return res;
            }
        }

        public static bool DeleteById(int id)
        {
            using (StudentContext db = new StudentContext())
            {
                var entity = db.StudentUnion.Find(id);
                if (null != entity)
                {
                    db.StudentUnion.Remove(entity);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static bool Update(StudentUnion entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.StudentUnion.Find(entity.UnionId);
                if (null == dbEntity)
                {
                    return false;
                }
                else
                {
                    dbEntity.StudentId = entity.StudentId;
                    dbEntity.Sex = entity.Sex;
                    dbEntity.Name = entity.Name;
                    dbEntity.Posision = entity.Posision;
                    dbEntity.Classes = entity.Classes;
                    dbEntity.College = entity.College;
                    dbEntity.Email = entity.Email;
                    dbEntity.DepartmentId = entity.DepartmentId;
                    db.SaveChanges();
                    return true;
                }
            }
        }
    }
}
